package web;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Map;

/**
 * @author CaiKe
 */
public abstract class BaseServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        System.out.println(request.getContentType());
        response.setContentType("application/json; charset=utf-8");
        String action = request.getParameter("action");
//        System.out.println(action);
//        Map<String, String[]> map = request.getParameterMap();
//        for (Map.Entry<String, String[]> item : map.entrySet()) {
//            System.out.println(item.getKey() + "=> " + Arrays.toString(item.getValue()));
//        }
        try {
            Method method = this.getClass().getDeclaredMethod(action, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(this, request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}